// 属性和方法
class Animal {
    public name
    constructor(name) {
        this.name = name
    }
    sayHi() {
        return 'My name is ' + this.name;
    }
}
// let a = new Animal('Jack');
// console.log(a.sayHi()); // My name is Jack


// 类的继承
class Cat extends Animal {
    constructor(name) {
        super(name)
        console.log(this.name);
    }
    sayHi() {
        return 'Cat ' + super.sayHi() // 调用父类的 sayHi()
    }
}
// let c = new Cat('Tom'); // Tom
// console.log(c.sayHi()); // Meow, My name is Tom


// 存取器
// 使用 getter 和 setter 可以改变属性的赋值和读取行为：
class XIAOJIEJIE {
    constructor(name) {
        this.name = name
    }
    get name() {
        return 'Jack';
    }
    set name(value) {
        console.log('setter: ' + value)
    }
}
// let a = new XIAOJIEJIE('Kitty'); // setter: Kitty
// console.log(a.name); //Jack



// 静态方法
class Person {
    static isAnimal(a) {
        return a instanceof Person;
    }
}
// let p = new Person();
// console.log(Person.isAnimal(p)); // true
// p.isAnimal(p); // TypeError: a.isAnimal is not a function



// ES7 中类的用法
// ES6 中实例的属性只能通过构造函数中的 this.xxx 来定义，ES7 提案中可以直接在类里面定义：
class Teacher {
    name = 'Jack'
    constructor() { }
}
let a = new Teacher();
// console.log(a.name); // Jack


// ES7 静态属性
class Tree {
    static uname = 'zs'
    constructor() { }
}
// console.log(Tree.uname); // 'zs'


// TypeScript 可以使用三种访问修饰符（Access Modifiers），分别是 public、private 和 protected
// readonly
class Animal1 {
    readonly name;
    public constructor(name) {
        this.name = name;
    }
}

// let b = new Animal1('Jack');
// console.log(b.name); // Jack
// b.name = 'Tom'; //error



//抽象类 
// 象类是不允许被实例化的
abstract class A {
    public name;
    public constructor(name) {
        this.name = name;
    }
    public abstract sayHi();
}

// let AB = new A('Jack'); //error


// 类的类型
class Name {
    public name: string
    constructor(name: string) {
        this.name = name
    }
    sayHi(): string {
        return `My name is ${this.name}`;
    }
}
let c: Name = new Name('Jack');
console.log(c.sayHi()); // My name is Jack

